package spindles.gwt.server;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;

import org.apache.commons.lang.time.DurationFormatUtils;

import spindles.api.db.DAO;
import spindles.api.db.DBGateway;
import spindles.api.domain.Audit;
import spindles.api.services.ImportService;
import spindles.api.util.ApplicationException;
import spindles.api.util.UserException;
import spindles.gwt.shared.AuditDTO;
import spindles.gwt.shared.GWTException;
import spindles.gwt.shared.ImportManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ImportServlet extends RemoteServiceServlet implements
		ImportManager {
	
	private transient DAO<Audit> auditDAO = new DAO<Audit>(Audit.class);
	private transient DBGateway db = new DBGateway();

	/**
	 * 
	 */
	private static final long serialVersionUID = -8293013958208517504L;

	@SuppressWarnings("unchecked")
	public List getAudits() {
		SortedSet<Audit> audits = auditDAO.findAllSorted(Audit.getComparator());

		List result = new ArrayList();

		for (Audit aud : audits) {
			AuditDTO dto = (AuditDTO) ServletUtil.map(aud, AuditDTO.class);
			dto.setEegDuration(DurationFormatUtils.formatDurationHMS(aud.getEegDuration() * 1000));
			dto.setImportDuration(DurationFormatUtils.formatDurationHMS(aud.getImportDuration() * 1000));
			result.add(dto);
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	public List getRecentAudits(int numberOfAudits) {
		List audits = getAudits();
		numberOfAudits = Math.min(numberOfAudits, getAudits().size()); 
		List result = new ArrayList();
		for (int i = 0; i < numberOfAudits; i++) {
			result.add(audits.get(i));
		}

		return result;
	}

	public void importSessionPartData(String filename, String channel,
			boolean allowDuplicatePerson) throws GWTException{
		try {
			if (isImporting()) {
				throw new ApplicationException(
						"Cannot run more than one import operation.");
			}
			ServletUtil.setImportStatus(true);
			
			_importSessionPartData(filename, channel, allowDuplicatePerson);
		} catch (Exception e) {
			throw new GWTException(e);
		}

//		Executor e = Executors.newSingleThreadExecutor();
//		e.execute(getRunnable(filename, channel, allowDuplicatePerson));

	}
	
	private void _importSessionPartData(String filename, String channel,
			boolean allowDuplicatePerson) throws UserException{
		ImportService imp = new ImportService(db.getDefaultThresholdGroup());
		try {
			imp.importSessionPartData(filename, channel,
					allowDuplicatePerson, false);
		} finally {
			ServletUtil.setImportStatus(false);
		}
	}

	public boolean isImporting() {
		return ServletUtil.isImporting();
	}

//	private Runnable getRunnable(final String filename, final String channel,
//			final boolean allowDuplicatePerson) {
//		return new Runnable() {
//
//			public void run() {
//				_importSessionPartData(filename, channel, allowDuplicatePerson);
//			}
//
//		};
//	}

}
